﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using ClassLibraryDatabase;

namespace ClassLibraryBaseClass
{
	/// <summary>
	///Session 的摘要说明
	/// </summary>
	public class SessionInfo
	{
		private static readonly string _connectionString = DataBaseClass.GetConnectionString();
		private static string[] m_sSessionInfoName = { "ID", "MovieID", "TheaterID",
                                                     "Time", "NTSC", "Price", "Hall"};
		/// <summary>
		/// ID属性及get方法
		/// </summary>
		private int _ID;
		public int ID
		{
			get { return _ID; }
		}
		/// <summary>
		/// MovieID属性及get方法
		/// </summary>
		private int _MovieID;
		public int MovieID
		{
			get { return _MovieID; }
		}
		/// <summary>
		/// TheaterID属性及get方法
		/// </summary>
		private int _TheaterID;
		public int TheaterID
		{
			get { return _TheaterID; }
		}
		/// <summary>
		/// 放映时间Time属性及get/set方法
		/// </summary>
		private DateTime _Time;
		public DateTime Time
		{
			get { return _Time; }
		}
		/// <summary>
		/// 电影播放类型属性NTSC及其get/set方法
		/// </summary>
		private string _NTSC;
		public string NTSC
		{
			get { return _NTSC; }
		}
		/// <summary>
		/// 票价Price及其get/set方法
		/// </summary>
		private decimal _Price;
		public decimal Price
		{
			get { return _Price; }
		}
		/// <summary>
		/// 放映厅Hall属性及其get/set方法
		/// </summary>
		private string _Hall;
		public string Hall
		{
			get { return _Hall; }
		}

		public SessionInfo(int id, int movieId, int theaterId, DateTime date, string ntsc
			, decimal price, string hall)
		{
			_ID = id;
			_MovieID = movieId;
			_TheaterID = theaterId;
			_Time = date;
			_NTSC = ntsc;
			_Price = price;
			_Hall = hall;
		}
		/// <summary>
		/// 添加一个电影场次到数据库
		/// </summary>
		/// <param name="id">电影场次编号</param>
		/// <param name="movieID">电影编号</param>
		/// <param name="theaterID">电影院编号</param>
		/// <param name="time">放映时间</param>
		/// <param name="ntsc">电影院网站链接</param>
		/// <param name="price">票价</param>
		/// <param name="hall">放映厅</param>
		/// <returns>插入是否成功</returns>
		public static bool AddSession(int id, int movieId, int theaterId, DateTime date
			, string ntsc, decimal price, string hall)
		{
			SessionInfo session = new SessionInfo(id, movieId, theaterId, date, ntsc, price, hall);
			return session.AddSelfInfo();
		}

		/// <summary>
		/// 保存自身到数据库
		/// </summary>
		/// <returns>插入是否成功</returns>
		public bool AddSelfInfo()
		{
			int nT = 0;

			if (this.Hall != null)
				++nT;

			nT += 5;
			string[] name = new string[nT];
			object[] value = new object[nT];

			//DataTable table = DataBaseClass.selectData(

			name[0] = "MovieID";
			value[0] = this.MovieID;

			name[1] = "TheaterID";
			value[1] = this.TheaterID;

			name[2] = "Time";
			value[2] = this.Time;

			name[3] = "NTSC";
			value[3] = this.NTSC;

			name[4] = "Price";
			value[4] = this.Price;

			if (this.Hall != null && this.Hall != "")
			{
				name[5] = "Hall";
				value[5] = this.Hall;
			}
			try
			{
				bool bT = DataBaseClass.insertData(name, value, "Session", _connectionString);
				this._ID = DataBaseClass.GetMaxID("Session", _connectionString);
				return bT;
			}
			catch (SqlException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}
		}
		/// <summary>
		/// 删除此电影场次
		/// </summary>
		/// <returns>删除是否成功</returns>
		public bool[] DeleteSelfInfo()
		{
			return DeleteItemFromDatabase(this.ID);
		}

		/// <summary>
		/// 删除与此电影信息相关的所有信息，在"Session"和"T_Comment"中
		/// </summary>
		/// <param name="nID"></param>
		/// <returns></returns>
		public static bool[] DeleteItemFromDatabase(int nID)
		{
			bool[] bT = new bool[2];
			bool[] bF = DeleteRelationship(nID);
			bT[1] = false;

			bT[0] = DataBaseClass.deleteData("ID", nID, "Session", _connectionString);
			return bT;
		}
		/// <summary>
		/// 删除与此电影信息相关的所有信息，在"Session"和"T_Comment"中
		/// </summary>
		/// <param name="nID"></param>
		/// <returns></returns>
		public static bool[] DeleteRelationship(int nID)
		{
			bool[] bT = { false };
			try
			{
				bT[0] = DataBaseClass.deleteData("SessionID", nID, "Order", _connectionString);
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
			}
			return bT;
		}

		/// <summary>
		/// 按照电影编号MovieID获得电影场次列表
		/// </summary>
		/// <param name="movieID">电影编号</param>
		/// <returns>电影场次Session对象列表List</returns>
		public static List<SessionInfo> GetSessionLsitByMovieID(int movieID)
		{
			List<SessionInfo> ls = new List<SessionInfo>();
			DataTable info = DataBaseClass.selectData("SELECT ID,TheaterID,Time," +
			"NTSC,Price,Hall FROM Session WHERE MovieID = " + movieID, _connectionString);
			for (int i = 0; i < info.Rows.Count; i++)
			{
				SessionInfo ses = new SessionInfo((int)info.Rows[i]["ID"], (int)info.Rows[i]["MovieID"]
					, (int)info.Rows[i]["TheaterID"], (DateTime)info.Rows[i]["Time"]
					, (string)info.Rows[i]["NTSC"], (decimal)info.Rows[i]["Price"]
					, (string)info.Rows[i]["Hall"]);
				ls.Add(ses);
			}
			return ls;
		}
		/// <summary>
		/// 按照电影编号MovieID获得电影场次数据表
		/// </summary>
		/// <param name="movieID">电影编号</param>
		/// <returns>电影场次Session对象数据表DataTable</returns>
		public static DataTable GetSessionTableByMovieID(int movieID)
		{
			//List<SessionInfo> ls = new List<SessionInfo>();
			DataTable info = DataBaseClass.selectData("SELECT * FROM Session" +
				" WHERE MovieID = " + movieID, _connectionString);
			/*for (int i = 0; i < info.Rows.Count; i++)
			{
				SessionInfo ses = new SessionInfo((int)info.Rows[i]["ID"], (int)info.Rows[i]["MovieID"]
					, (int)info.Rows[i]["TheaterID"], (DateTime)info.Rows[i]["Time"]
					, (string)info.Rows[i]["NTSC"], (double)info.Rows[i]["Price"]
					, (string)info.Rows[i]["Hall"]);
				ls.Add(ses);
			}*/

			return info;
		}
		/// <summary>
		/// 按照电影院编号获得电影场次数据表
		/// </summary>
		/// <param name="theaterID">电影院编号</param>
		/// <returns>DataTable对象</returns>
		public static DataTable GetSessionTableByTheaterID(int theaterID)
		{
			DataTable info = DataBaseClass.selectData("SELECT * FROM Session" +
				" WHERE TheaterID = " + theaterID, _connectionString);

			return info;
		}
		/// <summary>
		/// 按照放映时间获得电影场次数据表
		/// </summary>
		/// <param name="theaterID">放映时间</param>
		/// <returns>DataTable对象</returns>
		public static DataTable GetSessionTableByTime(DateTime time)
		{
			DataTable info = DataBaseClass.selectData("SELECT * FROM Session" +
				" WHERE DateTime = " + time, _connectionString);

			return info;
		}
		/// <summary>
		/// 按照票价获得电影场次数据表
		/// </summary>
		/// <param name="theaterID">票价</param>
		/// <returns>DataTable对象</returns>
		public static DataTable GetSessionTabelByPrice(double price)
		{
			DataTable info = DataBaseClass.selectData("SELECT * FROM Session" +
				" WHERE Price = " + price, _connectionString);

			return info;
		}
		public static bool DeleteSessionByMovieID(int nID)
		{
			try
			{
				DataBaseClass.deleteData("DELETE Order WHERE SessionID IN(SELECT ID FROM Session WHERE MovieID=" + nID + ")", _connectionString);
				return DataBaseClass.deleteData("MovieID", nID, "Session", _connectionString);
			}
			catch (SqlException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}

		}
		public static bool DeleteSessionByTheaterID(int nID)
		{
			try
			{
				//DataBaseClass.deleteData("DELETE Order WHERE SessionID IN(SELECT ID FROM Session WHERE TheaterID=" + nID + ")", _connectionString);
				return DataBaseClass.deleteData("TheaterID", nID, "Session", _connectionString);
			}
			catch (SqlException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("SessionInfo类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}

		}
		public static List<SessionInfo> GetListByDataTable(DataTable table)
		{
			List<SessionInfo> list = new List<SessionInfo>();
			foreach (DataRow row in table.Rows)
			{
				list.Add(GetSessionByRow(row));
			}
			return list;
		}
		public static SessionInfo GetSessionByRow(DataRow row)
		{
			return new SessionInfo((int)row["ID"], (int)row["MovieID"], (int)row["TheaterID"], (DateTime)row["Time"], row["NTSC"] as string, (decimal)row["Price"], row["Hall"] as string);
		}
	}

	public class Test6
	{
		public static void Test()
		{
			try
			{
				List<Movie> list = Movie.GetMovieListByDataTable(Movie.GetMovieByName("asdfar"));
				if (list.Count != 0)
					list[0].DeleteItselfFromDatabase();
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
			}
			try
			{
				Theater th = Theater.GetTheaterByName("DSF1asdf212");
				if (th != null)
					th.DeleteSelfInfo();
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
			}
			Movie movie = new Movie(-1, "asdfar", "asdf", "asdf", DateTime.Today, 120, "asdf", "asdf", "a3wr", 5.5M, null, "AEASDF");
			movie.AddThisItemToDatabase();
			Theater t = new Theater(-1, "DSF1asdf212", "ASDFasdfASF", "12fsd315", "546asf543.com", 5.0M, "AasdfSDF", 12);
			t.AddSelfInfo();

			SessionInfo info = new SessionInfo(-1, movie.ID, t.ID, DateTime.Today, "aaa", 50.0M, "213");
			info.AddSelfInfo();
			info = SessionInfo.GetSessionByRow(DataBaseClass.selectData("SELECT * FROM Session WHERE ID=" + info.ID, DataBaseClass.GetConnectionString()).Rows[0]);

			if (info.ID != DataBaseClass.GetMaxID("Session", DataBaseClass.GetConnectionString()))
				System.Console.WriteLine("ID出错");

			info.DeleteSelfInfo();
			int nT = DataBaseClass.selectData("SELECT * FROM Session WHERE ID=" + info.ID, DataBaseClass.GetConnectionString()).Rows.Count;
			if (nT != 0)
				System.Console.WriteLine("删除出错");

			info = new SessionInfo(-1, movie.ID, t.ID, DateTime.Today, "aaa", 50.0M, "213");
			info.AddSelfInfo();
			movie.DeleteItselfFromDatabase();
			nT = DataBaseClass.selectData("SELECT * FROM Session WHERE ID=" + info.ID, DataBaseClass.GetConnectionString()).Rows.Count;
			if (nT != 0)
				System.Console.WriteLine("联合删除出错");

			movie = new Movie(-1, "asdfar", "asdf", "asdf", DateTime.Today, 120, "asdf", "asdf", "a3wr", 5.5M, null, "AEASDF");
			movie.AddThisItemToDatabase();
			info = new SessionInfo(-1, movie.ID, t.ID, DateTime.Today, "aaa", 50.0M, "213");
			info.AddSelfInfo();
			t.DeleteSelfInfo();
			nT = DataBaseClass.selectData("SELECT * FROM Session WHERE ID=" + info.ID, DataBaseClass.GetConnectionString()).Rows.Count;
			if (nT != 0)
				System.Console.WriteLine("联合删除出错");

			movie.DeleteItselfFromDatabase();
			t.DeleteSelfInfo();
			info.DeleteSelfInfo();
		}
	}
}